/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TareasAleatoriasDialog.java
 *
 * Created on 02-may-2011, 23:47:05
 */

package ia_psr.transportesGUI;

import ia_psr.builder.GrafoEmpresaTransportesBuilder;
import ia_psr.builder.GraphBuilder;
import ia_psr.problemas.transportes.EmpresaTransportes;
import ia_psr.graph.Tarea;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Rubén Avalos Elvira y Fabiana Celiberti Tapie
 */
public class TareasAleatoriasDialog extends javax.swing.JFrame {
    private JPanel parent;

    /** Creates new form TareasAleatoriasDialog */
    public TareasAleatoriasDialog(JPanel parent) {
        this.parent = parent;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cbTipo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtTareasCantidad = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        btnTareasAceptar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        btnCancelar = new javax.swing.JButton();

        setTitle("ETaskScheduler - Generación aleatoria de tareas");

        cbTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Planificada", "No Planificada", "Ambas" }));

        jLabel2.setText("Tipo:");

        jLabel3.setText("Cantidad");

        btnTareasAceptar.setText("Aceptar");
        btnTareasAceptar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnTareasAceptarMouseReleased(evt);
            }
        });

        jLabel4.setText("Tareas aleatorias");

        btnCancelar.setText("Cancelar");
        btnCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnCancelarMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel3)
                                .addComponent(jLabel2))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtTareasCantidad)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(btnTareasAceptar)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnCancelar))
                                .addComponent(cbTipo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtTareasCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnTareasAceptar))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCancelarMouseReleased
        setVisible(false);
    }//GEN-LAST:event_btnCancelarMouseReleased

    private void btnTareasAceptarMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnTareasAceptarMouseReleased
        // TODO add your handling code here:
        try {
            int cantidad = Integer.parseInt(txtTareasCantidad.getText());
            ((TareasPanel)parent).clearTareasTable();
            Tarea t;
            GraphBuilder b = GrafoEmpresaTransportesBuilder.getInstance();
            b.buildGraph(); // elimina todas las tareas

            String nombre;
            String inputTipo = cbTipo.getSelectedItem().toString().toLowerCase();
            String tipo = inputTipo;
            int horaInicio;
            int horaFin;
            int duracion;
            EmpresaTransportes et = EmpresaTransportes.getInstance();
            Random r = new Random();
            r.setSeed(System.nanoTime());
            for (int i = 1; i <= cantidad; ++i) {
                nombre = "T" + i;
                if (inputTipo.equals("ambas")) {
                    tipo = (r.nextBoolean()) ? "planificada" : "no planificada";
                }

                if (tipo.equals("planificada")) {
                    // controlar que caiga en horario de la empresa
                    do
                        horaInicio = Math.abs(r.nextInt(24));
                    while ( horaInicio <  et.getHorarioEmpresa().horaInicio ||
                            horaInicio >= et.getHorarioEmpresa().horaFin-1 );
                    do
                        horaFin = horaInicio + Math.abs(r.nextInt(5)) + 1;
                    while (horaFin > et.getHorarioEmpresa().horaFin);
                    t = new Tarea(nombre, horaInicio, 0, horaFin, 0);

                } else if (tipo.equals("no planificada")) {
                    duracion = Math.abs(r.nextInt((et.horarioEmpresa.horaFin - et.horarioEmpresa.horaInicio)/2) + 1);
                    t = new Tarea(nombre, duracion);
                } else {
                    throw new Exception();
                }
                b.buildNode(t);
            }

            setVisible(false);
            ((TareasPanel)parent).populateTareasTable();

        } catch (Exception e) {
            // mostrar error dialog
            JFrame frame = new JFrame();
            JOptionPane.showMessageDialog(frame,
                    "Datos no válidos. Por favor, asegúrese de introducir una cantidad numérica.",
                    "Atención",
                    JOptionPane.WARNING_MESSAGE);
        }

    }//GEN-LAST:event_btnTareasAceptarMouseReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnTareasAceptar;
    private javax.swing.JComboBox cbTipo;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField txtTareasCantidad;
    // End of variables declaration//GEN-END:variables

}
